import axios from "axios";
import store from "@/store";
import snackbar from "./snackbar";

class HttpRequest {
    constructor (baseUrl = baseURL) {
        this.baseUrl = baseUrl
        this.queue = {}
    }
    getInsideConfig(url){
        const config = {
            baseURL: this.baseUrl,
            headers:{

            }
        }
        if(url !== 'login'){
            config.headers['Authorization'] = store.state.token
        }
        return config
    }
    destory (url){
        delete this.queue[url]
    }
    interceptors (instance, url){
        instance.interceptors.request.use(config => {
            this.queue[url] = true
            return config
        },error => {
            return Promise.reject(error)
        })
        instance.interceptors.response.use(res => {
            const {data,code,msg} = res.data
            if(code != 200){
                //提示错误信息
                snackbar.warning(msg)
            }else{
                if(msg != ''){
                    //提示系统信息
                    snackbar.success(msg)
                }
                return data;
            }
        })
    }
    request(options) {
        const instance = axios.create()
        options = Object.assign(this.getInsideConfig(options.url),options)
        this.interceptors(instance,options.url)
        return instance(options)
    }
}

export default HttpRequest